<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 内容管理插件
// +----------------------------------------------------------------------
namespace addons\cms;

use addons\cms\library\FulltextSearch;
use app\common\library\Menu;
use think\Addons;
use think\Db;
use think\facade\Config;
use think\facade\Route;

class Cms extends Addons
{
    protected $ext_table = '_data';

    //后台菜单
    protected $menu = [
        [
            "name"    => "cms",
            "title"   => "cms内容管理",
            "icon"    => "iconfont icon-draft-line",
            "sublist" => [
                [
                    "name"      => "addons/config/name/cms",
                    "title"     => "站点配置",
                    "icon"      => "iconfont icon-setup",
                    'ismenu'    => 1,
                    "listorder" => 10,
                ],
                [
                    "name"      => "cms.cms",
                    "title"     => "管理内容",
                    "icon"      => "iconfont icon-draft-line",
                    "listorder" => 9,
                    "sublist"   => [
                        ["name" => "cms.cms/index", "title" => "查看"],
                        ["name" => "cms.cms/add", "title" => "新增"],
                        ["name" => "cms.cms/edit", "title" => "编辑"],
                        ["name" => "cms.cms/del", "title" => "删除"],
                        ["name" => "cms.cms/listorder", "title" => "排序"],
                        ["name" => "cms.cms/remove", "title" => "批量移动"],
                        ["name" => "cms.cms/setstate", "title" => "状态"],
                        ["name" => "cms.cms/recycle", "title" => "回收站"],
                        ["name" => "cms.cms/destroy", "title" => "清空回收站"],
                        ["name" => "cms.cms/restore", "title" => "还原回收站"],
                    ],
                ],
                [
                    "name"      => "cms.publish",
                    "title"     => "稿件管理",
                    "icon"      => "iconfont icon-draft-line",
                    "listorder" => 8,
                    "sublist"   => [
                        ["name" => "cms.publish/index", "title" => "查看"],
                        ["name" => "cms.publish/del", "title" => "删除"],
                        ["name" => "cms.publish/pass", "title" => "通过"],
                        ["name" => "cms.publish/reject", "title" => "退稿"],
                    ],
                ],
                [
                    "name"      => "cms.order",
                    "title"     => "订单管理",
                    "icon"      => "iconfont icon-file-list-3-line",
                    "listorder" => 7,
                    "sublist"   => [
                        ["name" => "cms.order/index", "title" => "查看"],
                        ["name" => "cms.order/del", "title" => "删除"],
                    ],
                ],
                [
                    "name"      => "cms.tags",
                    "title"     => "Tags管理",
                    "icon"      => "iconfont icon-label",
                    "listorder" => 6,
                    "sublist"   => [
                        ["name" => "cms.tags/index", "title" => "查看"],
                        ["name" => "cms.tags/add", "title" => "新增"],
                        ["name" => "cms.tags/edit", "title" => "编辑"],
                        ["name" => "cms.tags/del", "title" => "删除"],
                        ["name" => "cms.tags/create", "title" => "数据重建"],
                        ["name" => "cms.tags/multi", "title" => "批量更新"],
                    ],
                ],
                [
                    "name"      => "cms.category",
                    "title"     => "栏目列表",
                    "icon"      => "iconfont icon-other",
                    "listorder" => 5,
                    "sublist"   => [
                        ["name" => "cms.category/index", "title" => "查看"],
                        ["name" => "cms.category/add", "title" => "新增"],
                        ["name" => "cms.category/cat_priv", "title" => "栏目授权"],
                        ["name" => "cms.category/edit", "title" => "编辑"],
                        ["name" => "cms.category/del", "title" => "删除"],
                        ["name" => "cms.category/multi", "title" => "批量更新"],
                    ],
                ],
                [
                    "name"      => "cms.models",
                    "title"     => "模型管理",
                    "icon"      => "iconfont icon-apartment",
                    "listorder" => 4,
                    "sublist"   => [
                        ["name" => "cms.models/index", "title" => "查看"],
                        ["name" => "cms.models/add", "title" => "新增"],
                        ["name" => "cms.models/edit", "title" => "编辑"],
                        ["name" => "cms.models/del", "title" => "删除"],
                        ["name" => "cms.models/multi", "title" => "批量更新"],
                    ],
                ],
                [
                    "name"    => "cms.field",
                    "title"   => "字段管理",
                    "ismenu"  => 0,
                    "sublist" => [
                        ["name" => "cms.field/index", "title" => "查看"],
                        ["name" => "cms.field/add", "title" => "新增"],
                        ["name" => "cms.field/edit", "title" => "编辑"],
                        ["name" => "cms.field/del", "title" => "删除"],
                        ["name" => "cms.field/listorder", "title" => "排序"],
                        ["name" => "cms.field/setstate", "title" => "状态"],
                        ["name" => "cms.field/setsearch", "title" => "状态-搜索"],
                        ["name" => "cms.field/setvisible", "title" => "状态-隐藏"],
                        ["name" => "cms.field/setrequire", "title" => "状态-必须"],
                    ],
                ],
            ],
        ],

    ];

    public $cache_list = [
        'Category' => [
            'name'   => '栏目索引',
            'model'  => 'Category',
            'action' => 'category_cache',
        ],
    ];

    //安装
    public function install()
    {
        $info = get_addon_info('member');
        if (!$info || $info['status'] != 1) {
            throw new \think\Exception("请在后台插件管理中安装《会员插件》并启用后再尝试");
        }
        Menu::create($this->menu);
        return true;
    }

    //卸载
    public function uninstall()
    {
        $droptables = request()->param("droptables");
        $auth       = \app\admin\service\User::instance();
        //只有开启调试且为超级管理员才允许删除相关数据库
        if ($droptables && Config::get("app_debug") && $auth->isAdministrator()) {
            // 删除模型中建的表
            $table_list = Db::name('model')->where('module', 'cms')->field('tablename,type,id')->select();
            if ($table_list) {
                foreach ($table_list as $val) {
                    $tablename = Config::get('database.prefix') . $val['tablename'];
                    Db::execute("DROP TABLE IF EXISTS `{$tablename}`;");
                    if ($val['type'] == 2) {
                        Db::execute("DROP TABLE IF EXISTS `{$tablename}{$this->ext_table}`;");
                    }
                    Db::name('model_field')->where('modelid', $val['id'])->delete();
                }
            }
            //删除模型中的表
            Db::name('model')->where(['module' => 'cms'])->delete();
        }
        Menu::delete("cms");
        return true;
    }

    /**
     * 插件启用方法
     * @return bool
     */
    public function enable()
    {
        Menu::enable("cms");
        return true;
    }

    /**
     * 插件禁用方法
     * @return bool
     */
    public function disable()
    {
        Menu::disable("cms");
        return true;
    }

    //或者run方法
    public function userSidenavAfter($content)
    {
        return $this->fetch('userSidenavAfter');
    }

    public function xunsearchIndexReset($project)
    {
        if ($project['name'] == 'cms') {
            return FulltextSearch::reset();
        }
    }

    /**
     * 插件升级方法
     */
    public function upgrade()
    {
        Menu::upgrade('cms', $this->menu);
        return true;
    }

    public function appInit()
    {
        $config = get_addon_config('cms');

        //TODO 暂时这么排除后台使用此路由
        //if (strpos(request()->url(), '/admin') === false) {
        Route::rule('', 'cms/index/index');
        Route::rule('index', 'cms/index/index');
        Route::rule('lists/:catid', 'cms/index/lists')->pattern(['catid' => '\d+']);
        Route::rule('shows/:catid/:id', 'cms/index/shows')->pattern(['catid' => '\d+', 'id' => '\d+']);
        Route::rule('tag/[:tag]', 'cms/index/tags');
        Route::rule('search', 'cms/search/index');
        if ($config['site_url_mode'] == 2) {
            Route::rule('d/:catdir/:id', 'cms/index/shows')->pattern(['catdir' => '[A-Za-z0-9\-\_]+', 'id' => '\d+']);
            Route::rule('c/:catdir/[:condition]', 'cms/index/lists')->pattern(['catdir' => '[A-Za-z0-9\-\_]+', 'condition' => '[0-9_&=a-zA-Z]+']);
        }
        //}

        //此函数需要全局调用
        if (is_file(ADDON_PATH . 'cms' . DS . 'function.php')) {
            include_once ADDON_PATH . 'cms' . DS . 'function.php';
        }
    }

    public function contentDeleteEnd($params)
    {
        //参数是审核文章的数据
        if (!empty($params) && isset($params['sysadd']) && $params['sysadd'] == 0) {
            //删除对应的会员投稿记录信息
            db("cms_content")->where(["content_id" => $params['id'], "catid" => $params['catid']])->delete();
        }
    }

    public function contentEditEnd($params)
    {
        //参数是审核文章的数据
        if (!empty($params)) {
            //标识审核状态
            db("cms_content")->where(["content_id" => $params['id'], "catid" => $params['catid']])->setField('status', $params['status']);
        }
    }

}
